﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.Supervise.IBLL;
using HZ.Supervise.Model;

public partial class TotalDrugs_Transaction_Detail : BasePage
{
    public TotalDrugs_Transaction_Detail()
    { }
    protected void Page_Load(object sender, EventArgs e)
    {
        QueryControl1.OnQueryClick += new EventHandler(QueryControl1_OnQueryClick);
        if (!this.IsPostBack)
        {
            ViewState["query"] = URLUtil.GetSuperviseQueryFromURL(Request);
            QueryControl1.BindDataPre((SuperviseQuery)ViewState["query"]);//绑定查询条件的一些数据源 质量层次等
            BindData();//绑定数据
            QueryControl1.BindSort(gvwInfo);
            GetDetailCount();
        }
    }

    void QueryControl1_OnQueryClick(object sender, EventArgs e)
    {
        AspNetPager1.CurrentPageIndex = 1;
        ViewState["query"] = QueryControl1.UpdateQueryFromValue((SuperviseQuery)ViewState["query"]);
        BindData();
        GetDetailCount();
    }

    private void BindData()
    {
        int t = 0;
        IList<HZ.Supervise.Model.Supervise> CompanyList;
        HZ.Supervise.BLL.BLL_Statistics Bll_sta = (HZ.Supervise.BLL.BLL_Statistics)HZ.Supervise.BLLFactory.IBLL_Statistics();
        HZ.Supervise.Model.SuperviseQuery SuperWhere = (SuperviseQuery)ViewState["query"];

        SuperWhere.SortFields = QueryControl1.SortField == "" ? "ProductName ASC" : QueryControl1.SortField;

        CompanyList = Bll_sta.GetTradeInfo(SuperWhere, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out t);
        AspNetPager1.RecordCount = t;
        this.gvwInfo.DataSource = CompanyList;
        this.gvwInfo.DataBind();
    }


    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        BindData();
    }


    #region   数据准备

    #endregion

    protected void btnExcel_Click(object sender, EventArgs e)
    {
        int t = 0;
        IList<HZ.Supervise.Model.Supervise> CompanyList;
        HZ.Supervise.BLL.BLL_Statistics Bll_sta = (HZ.Supervise.BLL.BLL_Statistics)HZ.Supervise.BLLFactory.IBLL_Statistics();
        HZ.Supervise.Model.SuperviseQuery SuperWhere = (SuperviseQuery)ViewState["query"];

        SuperWhere.SortFields = QueryControl1.SortField == "" ? "ProcureCatalogID ASC" : QueryControl1.SortField;

        CompanyList = Bll_sta.GetTradeInfo(SuperWhere, 0, 0, out t);

        System.Data.DataTable dt = HZ.Data.ModelConvert.GetDataTableFromModel<HZ.Supervise.Model.Supervise>(CompanyList);

        ExportFile.Instance.ExportData(dt, gvwInfo, this.Page);

    }
    public void GetDetailCount()
    {
        int t = 0;
        IList<HZ.Supervise.Model.Supervise> CompanyList;
        HZ.Supervise.BLL.BLL_Statistics Bll_sta = (HZ.Supervise.BLL.BLL_Statistics)HZ.Supervise.BLLFactory.IBLL_Statistics();
        HZ.Supervise.Model.SuperviseQuery SuperWhere = (SuperviseQuery)ViewState["query"];

        SuperWhere.SortFields = QueryControl1.SortField == "" ? "ProductName ASC" : QueryControl1.SortField;

        CompanyList = Bll_sta.GetTradeInfo(SuperWhere, 1, 0, out t);
        int PurchaseCount = 0;
        int DistributeCount = 0;
        int WarehouseCount = 0;
        decimal PurchaseAmount = 0;
        decimal WarehouseAmount = 0;
        if (CompanyList.Count > 0)
        {
            foreach (var item in CompanyList)
            {
                if (item.PurchaseCount.HasValue)
                {
                    PurchaseCount += item.PurchaseCount.Value;
                }
                if (item.DistributeCount.HasValue)
                {
                    DistributeCount += item.DistributeCount.Value;
                }
                if (item.WarehouseCount.HasValue)
                {
                    WarehouseCount += item.WarehouseCount.Value;
                }
                if (item.PurchaseCount.HasValue)
                {
                    PurchaseAmount += item.PurchaseAmount.Value;
                }
                if (item.WarehouseAmount.HasValue)
                {
                    WarehouseAmount += item.WarehouseAmount.Value;
                }
            }
            lblCount.Text = "采购总数量为:" + PurchaseCount.ToString() + ";配送总数量为:" + DistributeCount.ToString() + ";入库总数量为:" + WarehouseCount.ToString() + ";采购总金额为:" + PurchaseAmount.ToString("N2") + ";入库总金额为:" + WarehouseAmount.ToString("N2") + "";
        }
        else
        {
            lblCount.Text = "暂无符合条件的数据！";
        }
    }
}
